package com.srwl.mytx.widget;


import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

import com.bm.library.Info;
import com.srwl.mytx.R;
import com.srwl.mytx.activity.MyFileSelectActivity;
import com.srwl.mytx.domain.MyFile;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 不再用这个dialog ,改用了{@link MyFileSelectActivity}
 */
@Deprecated
public class SelectFilePreviewDialog extends DialogFragment implements PreviewSelectFilePageView.AnimationListener {
    private static final String TAG = "SelectFilePreviewDialog";
    public static final String FILE_LIST = "FILE_LIST";

    PreviewSelectFilePageView previewSelectFilePageView;
    private ArrayList<MyFile> fileList;
    private int position;
    private Info info;
    private PreviewSelectFilePageView.GetPhotoViewInfoListener getPhotoViewInfoListener;


    public static SelectFilePreviewDialog newInstance(ArrayList<MyFile> fileList, int position, Info info) {
        SelectFilePreviewDialog myFileRepositoryDialog = new SelectFilePreviewDialog();
        Bundle bundle = new Bundle();
        bundle.putParcelableArrayList(FILE_LIST, fileList);
        bundle.putInt("position", position);
        bundle.putParcelable("info", info);
        bundle.putParcelableArrayList(FILE_LIST, fileList);
        myFileRepositoryDialog.setArguments(bundle);
        return myFileRepositoryDialog;
    }


    public static SelectFilePreviewDialog newInstance() {
        return new SelectFilePreviewDialog();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // int theme = android.R.style.Theme_Holo_Light_Dialog;
        int theme = R.style.dialog_full_screen;
        setStyle(DialogFragment.STYLE_NO_TITLE, theme);
        if (getArguments() != null) {
            fileList = getArguments().getParcelableArrayList(FILE_LIST);
            position = getArguments().getInt("position");
            info = getArguments().getParcelable("info");
        }

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        Log.d(TAG, "onCreateView: 执行了，，，");
        View view = inflater.inflate(R.layout.select_file_preview_dialog, container, false);
        previewSelectFilePageView = view.findViewById(R.id.preview_file);
        previewSelectFilePageView.init(fileList, null, false);
        previewSelectFilePageView.showBigPictures(position, info);
        previewSelectFilePageView.setAnimationListener(this);
        previewSelectFilePageView.setGetPhotoViewInfoListener(getPhotoViewInfoListener);
        this.getDialog().setOnKeyListener(new DialogInterface.OnKeyListener() {

            @Override
            public boolean onKey(DialogInterface arg0, int keyCode, KeyEvent arg2) {

                if (keyCode == KeyEvent.KEYCODE_BACK) {
                    onBackPress();
                    return true;
                }
                return false;
            }
        });
        return view;
    }


    public void setGetPhotoViewInfoListener(PreviewSelectFilePageView.GetPhotoViewInfoListener getPhotoViewInfoListener) {
        this.getPhotoViewInfoListener = getPhotoViewInfoListener;
        //不能直接调用previewSelectFilePageView，此刻它还是空的。
        // previewSelectFilePageView.setGetPhotoViewInfoListener(getPhotoViewInfoListener);
    }

    @Override
    public void onStart() {
        super.onStart();
        Window win = getDialog().getWindow(); // 一定要设置Background，如果不设置，window属性设置无效
        win.setBackgroundDrawable(new ColorDrawable(Color.WHITE));
        DisplayMetrics dm = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
        WindowManager.LayoutParams params = win.getAttributes();
        // params.gravity = Gravity.CENTER; // 使用ViewGroup.LayoutParams，以便Dialog 宽度充满整个屏幕
        params.width = ViewGroup.LayoutParams.MATCH_PARENT;
        params.height = ViewGroup.LayoutParams.MATCH_PARENT;
        win.setAttributes(params);
    }

    public void onBackPress() {
        previewSelectFilePageView.onBackPress();
    }

    @Override
    public void onAnimationEnd() {
        dismiss();
    }


    public interface InteractionListener {
        void onConfirm(List<MyFile> videoFiles, List<MyFile> pictureFiles);
    }
}
